//
//  BMSinglePickerView.m
//  bigmama
//
//  Created by huyuchen on 2017/8/9.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMSinglePickerView.h"



extern void ifDebug(dispatch_block_t blcok);

@interface BMSinglePickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) BMPickerViewToolBar *toolBar;
@property (strong, nonatomic) NSArray<NSString *> *data;
@property (assign, nonatomic) NSInteger selectedIndex;
@property (strong, nonatomic) NSString *selectedValue;
@property (copy, nonatomic) SingleSelectedBlock selectedValuedChangeBlock;


@end

@implementation BMSinglePickerView

- (instancetype)initWithToolBarText:(NSString *)toolBarText defaultIndex:(NSInteger)defaultIndex data:(NSArray<NSString *> *)data toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle valueDidChangedBlock:(SingleSelectedBlock)valueDidChangedBlock cancelBlock:(BtnClickBlock)cancelBlock doneBlock:(SingleClickBlock)doneBlock{
    
    if (self = [super init]) {
        
        if (!data || ![data isKindOfClass:[NSArray class]]) {
            ifDebug(^{
                NSAssert(NO, @"设置的数据格式不正确, 初始化失败");
            });
            
            return nil;
        }
        
        _data = data;
        _selectedValuedChangeBlock = [valueDidChangedBlock copy];
        
        if (defaultIndex < 0 || defaultIndex >= data.count) {
            ifDebug(^{
                NSLog(@"设置的默认下标不合法, 将被设置为0");
            });
            defaultIndex = 0;
        }
        _selectedIndex = defaultIndex;
        
        /// 验证完毕, 选择值改变
        [self setupSelectedValueDidChanged];
        
        __weak typeof(self) weakSelf = self;
        
        _toolBar = [[BMPickerViewToolBar alloc] initWithToolbarText:toolBarText style:toolBarStyle cancelBlock:cancelBlock doneBlock:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                if (doneBlock) {
                    doneBlock(strongSelf.selectedIndex, strongSelf.selectedValue);
                }
            }
        }];
        
        /// 滚动到默认的位置
        [self.pickerView selectRow:_selectedIndex inComponent:0 animated:NO];
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.pickerView];
        [self addSubview:_toolBar];
        [self setupUI];
    }
    
    return self;
    
}

- (void)setupUI{
    CGFloat kToolBarHeight = 44.0f;
    
    [self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(44);
    }];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(260-kToolBarHeight);
    }];
    
//    NSLayoutConstraint *toolBarLeft = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f];
//    NSLayoutConstraint *toolBarRight = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0f];
//    
//    NSLayoutConstraint *toolBarHeight = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:kToolBarHeight];
//    NSLayoutConstraint *toolBarTop = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0f];
//    self.toolBar.translatesAutoresizingMaskIntoConstraints = NO;
//    [self addConstraints:@[toolBarLeft, toolBarRight, toolBarHeight, toolBarTop]];
//    
//    NSLayoutConstraint *pickerViewLeft = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f];
//    NSLayoutConstraint *pickerViewRight = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0f];
//    
//    NSLayoutConstraint *pickerViewHeight = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.bounds.size.height - kToolBarHeight];
//    NSLayoutConstraint *pickerViewBottom = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0f];
//    self.pickerView.translatesAutoresizingMaskIntoConstraints = NO;
//    [self addConstraints:@[pickerViewLeft, pickerViewRight, pickerViewHeight, pickerViewBottom]];
}

- (void)layoutSubviews {
    [super layoutSubviews];

}

- (void)setupSelectedValueDidChanged {
    if (_selectedValuedChangeBlock) {
        _selectedValuedChangeBlock(self.selectedValue);
    }
}

#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.data.count;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _selectedIndex = row;
    [self setupSelectedValueDidChanged];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [UILabel new];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = Color_RGB(80, 60, 40);
    label.font = [UIFont systemFontOfSize:20.0f];
    label.backgroundColor = [UIColor clearColor];
    label.adjustsFontSizeToFitWidth = YES;
    if ([[self.data objectAtIndex:row] isKindOfClass:[NSString class]]) {
        label.text = self.data[row];
    }
    return label;
}

#pragma mark - setter getter
- (NSString *)selectedValue {
    if (_selectedIndex >= 0 && _selectedIndex < self.data.count) {
        _selectedValue = self.data[_selectedIndex];
    }
    return _selectedValue;
}

- (NSArray<NSString *> *)data {
    if (_data == nil) {
        _data = [NSArray array];
    }
    return _data;
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

- (void)dealloc{
    NSLog(@"BMSinglePickerView dealloc");
}

@end
